package com.bestcem.xm.common.core.sequence.dao.impl;

import com.bestcem.xm.common.core.sequence.dao.SequenceDao;
import com.bestcem.xm.common.core.sequence.dao.mapper.SequenceMapper;
import com.bestcem.xm.common.core.sequence.entity.SequenceDO;
import com.bestcem.xm.common.core.sequence.entity.SequenceEntity;
import com.bestcem.xm.common.core.uitls.DateUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 序号 dao
 *
 * @author yf.wang <yf.wang@idiaoyan.com>
 * @version V1.0
 * @date 2021/7/31
 */

@Repository
public class SequenceDaoImpl implements SequenceDao {

    @Resource
    private SequenceMapper sequenceMapper;

    @Override
    public SequenceDO findOneByCode(String code) {
        SequenceEntity entity = sequenceMapper.findOneByCode(code);
        return toDO(entity);
    }

    @Override
    public SequenceDO findOneAndUpdateByCode(SequenceEntity sequence) {
        // 新增
        sequence.setId(StringUtil.getId());
        sequenceMapper.createSequence(sequence);
        SequenceEntity entity = sequenceMapper.findOneByCode(sequence.getCode());
        return toDO(entity);
    }

    @Override
    public void findOneAndUpdateByCodeAndSeq(String code, Integer seq, Integer incrStep) {
        // 根据 code获取序号
        SequenceEntity sequenceEntity = sequenceMapper.findOneByCode(code);
        // 更新序号
        SequenceEntity updateSequence = new SequenceEntity();
        updateSequence.setSeq(incrStep);
        updateSequence.setId(sequenceEntity.getId());
        updateSequence.setUpdateTime(DateUtil.getCommonDate());
        // 更新
        //sequenceMapper.updateSeqById(updateSequence);
        sequenceMapper.updateById(updateSequence);
    }

    @Override
    public void findOneAndUpdateByCodeAndSeqInc(String code, int incr) {
        // 根据 code获取序号
        SequenceEntity sequenceEntity = sequenceMapper.findOneByCode(code);
        // 更新序号
        SequenceEntity updateSequence = new SequenceEntity();
        updateSequence.setSeq(sequenceEntity.getSeq() + incr);
        updateSequence.setId(sequenceEntity.getId());
        updateSequence.setUpdateTime(DateUtil.getCommonDate());
        // 更新
        //sequenceMapper.updateSeqById(updateSequence);
        sequenceMapper.updateById(updateSequence);
    }

    @Override
    public void updateSeqByCode(Integer seq, String code) {
        sequenceMapper.updateSeqByCode(seq, code);
    }

    @Override
    public void batchCreate(ArrayList<SequenceEntity> createList) {
        sequenceMapper.batchCreate(createList);
    }

    @Override
    public void batchUpdateSeq(List<SequenceEntity> sequenceList) {
        sequenceMapper.batchUpdateSeq(sequenceList);
    }

    @Override
    public ArrayList<SequenceEntity> listByCodes(ArrayList<String> keys) {
        return sequenceMapper.listByCodes(keys);
    }

    /**
     * SequenceEntity to SequenceDo
     *
     * @param entity
     * @return
     */
    private SequenceDO toDO(SequenceEntity entity) {
        if (entity == null) {
            return null;
        }
        SequenceDO sequenceDo = new SequenceDO();
        sequenceDo.setCode(entity.getCode());
        sequenceDo.setCreateTime(entity.getCreateTime());
        sequenceDo.setId(entity.getId());
        sequenceDo.setIncrStep(entity.getIncrStep());
        sequenceDo.setSeq(entity.getSeq());
        sequenceDo.setUpdateTime(entity.getUpdateTime());
        return sequenceDo;
    }
}
